<?php
namespace app\wypadmin\controller;
use think\Request;

/**
 * 房源类别
 */
class Cate extends Base
{
	private $cate;

	public function _initialize(){
		parent::_initialize();
		$this->cate = Model('Category');
	}
/**
 *  递归获取所有分类
 */
    public function menuTree($arr,$pid=0,$level=0){
        static $tree = array();
        foreach ($arr as $v) {
            if ( $v['pid'] == $pid) {
                $v['level'] = $level;
                $v['catename'] = '├ ' . str_repeat('---- ',$level) . $v['catename'];
                $tree[] = $v;
                $this->menuTree($arr,$v['id'],$level+1);
            }
        }
        return $tree;
    }

	public function add(){
		$list 		= $this->cate->getList();
		$list 		= $this->menuTree($list);
		$option 	= '';

		foreach ($list as $v) {
			if ( $v['level'] == 1) {
				$option .= '<option value="' . $v['id'] .'" disabled>' . $v['catename'] . '</option>';
			}else{
				$option .= '<option value="' . $v['id'] .'">' . $v['catename'] . '</option>';
			}			
		}
		$this->assign('option',$option);
		$this->assign('webname',$this->webname);
		return $this->fetch();
	}

	public function savecate(){
		$request = Request::instance();

		if (!$request->isPost()) {
			echo '页面不存在！';
			return;
		}

		$data['id'] 		= $request->post('id');
		$data['catename'] 	= $request->post('catename');
		$data['pid'] 		= $request->post('pid');
		$data['adddate']	= time();

		if ($data['id']) {
			$res = $this->cate->save($data,['id'=>$data['id']]);
			if ($res) {
				echo '<script>alert("更新完成");location.href="' . url('lst') . '"</script>';
			}else{
				echo '<script>alert("更新失败");history.back();"</script>';
			}
		}else{
			$res = $this->cate->save($data);
			if ($res) {
				echo '<script>alert("添加完成");location.href="' . url('add') . '"</script>';
			}else{
				echo '<script>alert("添加失败");history.back();"</script>';
			}
		}

	}

	public function edit($id){
		$info	= $this->cate->getOne($id);
		
		$list 		= $this->cate->getList();
		$list 		= $this->menuTree($list);
		$option 	= '<option value="0" selected="selected">根类别</option>';

		foreach ($list as $v) {
			if ( $v['level'] == 1) {
				$option .= '<option value="' . $v['id'] .'" disabled>' . $v['catename'] . '</option>';
			}else{
				$option .= '<option value="' . $v['id'] .'">' . $v['catename'] . '</option>';
			}			
		}
		$this->assign([
			'info' 		=> $info,
			'option' 	=> $option
		]);
		$this->assign('webname',$this->webname);
		return $this->fetch();
	}

	public function lst(){
		$list = $this->cate->getList();
		$list 		= $this->menuTree($list);
		$tr = '';
		foreach ($list as $v) {
			$tr .= '<tr>
						<td>' . $v['catename'] . '</td>
						<td>
							<a href="' . url('edit','id=' .$v['id']) . '"><span class="glyphicon glyphicon-pencil"></span>编辑</a> | 
							<a href="javascript:del(' . $v['id'] . ');"><span class="glyphicon glyphicon-trash"></span>删除</a>
						</td>
					</tr>';
		}
		$this->assign('tr',$tr);
		$this->assign('webname',$this->webname);
		return $this->fetch();
	}

	public function del($id){
		$where = [
			'id' => $id,
		];
		$res = $this->cate->where($where)->delete();
		if ($res) {
			echo '<script>alert("删除完成");location.href="' . url('lst') . '"</script>';
		}else{
			echo '<script>alert("删除失败");history.back();"</script>';
		}
	}
}